package com.dhcc.hikari.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.dhcc.hikari.dao.NurseRevocationDao;
import com.dhcc.hikari.entity.NurseRevocation;
import com.dhcc.hikari.mapper.NurseRevocationMapper;
import com.dhcc.hikari.service.NurseRevocationService;

/**
* <p>标题: NurseRevocationServiceImpl.java</p>
* <p>业务描述:健康管理平台</p>
* <p>公司:东华软件股份公司</p>
* <p>版权:dhcc2013</p>
* @author 胡凯
* @date 2019年9月3日
* @version V1.0 
*/
@Service
public class NurseRevocationServiceImpl implements NurseRevocationService {
	@Autowired
	private NurseRevocationDao nurseRevocationDao;
    @Autowired
    private NurseRevocationMapper nurseRevocationMapper;
	@Override
	public void save(NurseRevocation nurseRevocation) {
		nurseRevocationDao.save(nurseRevocation);
	}

	@Override
	public void update(NurseRevocation nurseRevocation) {
		nurseRevocationDao.save(nurseRevocation);
	}

	@Override
	public NurseRevocation findById(String id) {
		return nurseRevocationDao.findById(id).get();
	}

	@Override
	public void batchSave(List<NurseRevocation> nurseRevocations) {
		nurseRevocationDao.saveAll(nurseRevocations);
	}
	@Override
	public int batchInsert(List<NurseRevocation> nurseRevocations) {
		return nurseRevocationMapper.insertForeach(nurseRevocations);
	}

	@Override
	public Page findAll(NurseRevocation nurseRevocation, int page, int size) {
		// 1.需要查询条件
		Specification<NurseRevocation> spec = new Specification<NurseRevocation>() {
			/**
			 * 动态拼接查询条件
			 * 
			 * @return
			 */
			public Predicate toPredicate(Root<NurseRevocation> root, CriteriaQuery<?> criteriaQuery,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> list = new ArrayList<>();
				// 根据请求的companyId是否为空构造查询条件
				if (!StringUtils.isEmpty(nurseRevocation.getIdCard())) {
					list.add(criteriaBuilder.equal(root.get("idCard").as(String.class), (String) nurseRevocation.getIdCard()));
				}
				return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
			}
		};
		// 2.分页
		Page<NurseRevocation> pageNrs = nurseRevocationDao.findAll(spec, new PageRequest(page - 1, size));
		return pageNrs;
	}

	@Override
	public List<NurseRevocation> findList(NurseRevocation nurseRevocation) {
		return nurseRevocationDao.findAll();
	}
	

}
